package com.hamu.controller;

import com.hamu.pojo.Article;
import com.hamu.pojo.Result;
import com.hamu.service.ArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleTag")
public class ArticleTagController {
    @Autowired
    private ArticleTagService articleTagService;

    // 给文章添加标签
    @PostMapping("/{articleId}/tags")
    public Result addTags(
            @PathVariable Integer articleId,
            @RequestParam List<Integer> tagIds) {
        articleTagService.addTagToArticle(articleId, tagIds);
        return Result.success();
    }

    // 从文章移除标签
    @DeleteMapping("/{articleId}/tags")
    public Result removeTags(
            @PathVariable Integer articleId,
            @RequestParam List<Integer> tagIds) {
        articleTagService.removeTagFromArticle(articleId, tagIds);
        return Result.success();
    }

    // 新增删除所有标签接口
    @DeleteMapping("/{articleId}/tags/all")
    public Result removeAllTags(@PathVariable Integer articleId) {
        articleTagService.removeAllTagsFromArticle(articleId);
        return Result.success();
    }


    // 根据标签搜索文章
    @GetMapping("/by-tag")
    public Result<List<Article>> getArticlesByTag(
            @RequestParam(name = "tagName", required = false) List<String> tagNames,
            @RequestParam(name = "tagId", required = false) List<Integer> tagIds) {
        List<Article> articles = articleTagService.getArticlesByTag(tagNames, tagIds);
        return Result.success(articles);
    }

    @PutMapping("/{articleId}/tags")
    public Result updateTags(
            @PathVariable Integer articleId,
            @RequestParam List<Integer> tagIds) {

        articleTagService.updateArticleTags(articleId, tagIds);
        return Result.success("标签更新成功");
    }
}
